
<!DOCTYPE html> 
 <html xmlns="http://www.w3.org/1999/xhtml"> 
 <head> 
     <title>Simple Single Page</title> 
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"> 
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">       
 </head> 
 <body role="document">
 <div class="container" role="main">
       <div class="row">
           <table class="table table-striped table-bordered table-condensed table-hover">
                <thead>
                    <tr>
                        <th>Label</th>
                        <th>Value</th>
                    </tr>
                </thead>
                <tbody class="data-container">
                    <tr>
                        <td class="view-data-label"></td>
                        <td class="view-data-value"></td>
                    </tr>
                </tbody>
            </table>
       </div>   
  </div> 
 
    <script src="ClientGlobalContext.js.aspx" type="text/javascript" ></script>
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
    <script src="https://secure.aadcdn.microsoftonline-p.com/lib/1.0.0/js/adal.min.js"></script>
    <script src="https://www.promisejs.org/polyfills/promise-7.0.4.min.js"></script>
<!--    <script src="https://xrmtoolscode.azureedge.net/CRMWebAPI.js"></script> -->
    <script src="..\..\lib\CRMWebAPI.js"></script>     
    <script>
        function PopulateOutputData(result)
        {
            var $template = $(".data-container");
                var output = '';
        
                for (var property in result) {
                    if (result.hasOwnProperty(property)) {
                        var $entry = $template;
                        $entry.find(".view-data-label").html(property);
                        $entry.find(".view-data-value").html(result[property]);
                        output += $entry.html();
                    }
                }
                var $dataContainer = $(".data-container");
                $dataContainer.empty();
                $dataContainer.html(output);
        }
        
        // For Outside CRM Testing 
        //var apiconfig = { APIUrl: 'https://orgname.crm.dynamics.com' + '/api/data/v8.0/',  AccessToken:''};
        
        var apiconfig = { APIUrl: Xrm.Page.context.getClientUrl() + '/api/data/v8.0/'};
        
        var crmAPI = new CRMWebAPI(apiconfig);
        var userid = null;
                        
        crmAPI.ExecuteFunction("WhoAmI").then(function(result)        
        {
            var queryOptionsUser = {
                FormattedValues:true,
                Expand:[{Property:'businessunitid', Select:['businessunitid','name','websiteurl']}]
            };
            
            crmAPI.Get("systemusers",result.UserId,queryOptionsUser).then(function (buresult)
            {
                PopulateOutputData(result);
                
                var queryOptionsBU = {
                  FormattedValues:true,
                    Expand:[
                        {Property:'business_unit_system_users', 
                         Select:['systemuserid','fullname'], 
                         Filter:'systemuserid ne ' + result.UserId, 
                         OrderBy:['createdon asc'],
                         Top:5
                        }]
                };    
                crmAPI.Get("businessunits",buresult.businessunitid.businessunitid,queryOptionsBU).then(function (busersresult)
                {
                    console.log("retrieved business unit");
                });
            });
            
        });
            
    </script>
    </body>
 </html>